package com.rongxun.hbwallet.mvp.presenter;

import android.content.Context;
import android.support.annotation.NonNull;

import com.rongxun.base.BaseObserver;
import com.rongxun.base.BasePresenter;
import com.rongxun.hbwallet.R;
import com.rongxun.hbwallet.model.bean.ImageCode;
import com.rongxun.hbwallet.model.entity.PostFindPasswordInfo;
import com.rongxun.hbwallet.model.entity.PostMobileInfo;
import com.rongxun.hbwallet.model.bean.User;
import com.rongxun.hbwallet.mvp.contract.IForget;
import com.rongxun.utils.ALog;
import com.rongxun.utils.JsonUtils;
import com.rongxun.utils.MyToast;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 创建者     庄丰泽
 * 创建时间   2018/8/17 14:29
 * 描述
 */
public class ForgetPresenter extends BasePresenter<IForget.IView,IForget.IModel> implements IForget.IPresenter {
    public ForgetPresenter(@NonNull IForget.IView pUi, @NonNull IForget.IModel pModel) {
        super(pUi, pModel);
    }

    @Override
    public void release() {

    }

    @Override
    public void getImageCode(String mobile) {
        if(mobile.isEmpty()){
            getView().showError(getView().getContext().getString(R.string.input_phone),"");
            return;
        }
        addSubscribe(getModel().checkImgVerificationCode().subscribeWith(new BaseObserver<ImageCode>(){
            @Override
            public void onSuccess(ImageCode code, Context context) {
                getView().showImageView(code);
            }}
        ));
    }

    @Override
    public void getMobileNumber(String captchaCode, String captchaId, String country, String mobile) {
        if(captchaCode.isEmpty()||mobile.isEmpty()){
            getView().showError(getView().getContext().getString(R.string.input_mobile_code),"");
            return;
        }
        PostMobileInfo info=new PostMobileInfo();
        info.setCaptchaCode(captchaCode);
        info.setCaptchaId(captchaId);
        info.setCountry(country);
        info.setMobile(mobile);

        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), JsonUtils.toJson(info));
        addSubscribe(getModel().getMobileCode(requestBody).subscribeWith(new BaseObserver<User>(getView()){
            @Override
            public void onSuccess(User code, Context context) {
                String hint=getView().getContext().getString(R.string.successfully);
                MyToast.showToast(getView().getContext(),hint);
            }}
        ));
    }

    @Override
    public void findPassword(String captcha, String mode, String newPassword, String oldPassword) {
        if(!newPassword.equals(oldPassword)){
            getView().showError(getView().getContext().getString(R.string.tow_password_the_same),"");
            return;
        }
        PostFindPasswordInfo info=new PostFindPasswordInfo();
        info.setCaptcha(captcha);
        info.setMode(mode);
        info.setNewPassword(newPassword);
        ALog.i("Forget",JsonUtils.toJson(info));
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), JsonUtils.toJson(info));
        addSubscribe(getModel().findPassword(requestBody).subscribeWith(new BaseObserver<User>(getView()){
            @Override
            public void onSuccess(User code, Context context) {
                String hint=getView().getContext().getString(R.string.successfully);
                MyToast.showToast(getView().getContext(),hint);
            }}
        ));
    }
}
